package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.excel.DefautExcelResult;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.ma.domain.MaMaterialSupplier;
import com.ruoyi.ma.domain.vo.MaMaterialSupplierVo;
import com.ruoyi.ma.mapper.MaMaterialSupplierMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaMaterialManifestBo;
import com.ruoyi.ma.domain.vo.MaMaterialManifestVo;
import com.ruoyi.ma.domain.MaMaterialManifest;
import com.ruoyi.ma.mapper.MaMaterialManifestMapper;
import com.ruoyi.ma.service.IMaMaterialManifestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 材料清单Service业务层处理
 *
 * @author llb
 * @date 2024-11-01
 */
@RequiredArgsConstructor
@Service
public class MaMaterialManifestServiceImpl implements IMaMaterialManifestService {

    private final MaMaterialManifestMapper baseMapper;
    private final MaMaterialSupplierMapper supplierMapper;

    /**
     * 查询材料清单
     */
    @Override
    public MaMaterialManifestVo queryById(Long manifestId){
        return baseMapper.selectVoById(manifestId);
    }

    /**
     * 查询材料清单列表
     */
    @Override
    public TableDataInfo<MaMaterialManifestVo> queryPageList(MaMaterialManifestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaMaterialManifest> lqw = buildQueryWrapper(bo);
        Page<MaMaterialManifestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询材料清单列表
     */
    @Override
    public List<MaMaterialManifestVo> queryList(MaMaterialManifestBo bo) {
        LambdaQueryWrapper<MaMaterialManifest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaMaterialManifest> buildQueryWrapper(MaMaterialManifestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaMaterialManifest> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBdId() != null, MaMaterialManifest::getBdId, bo.getBdId());
        lqw.eq(bo.getZdId() != null, MaMaterialManifest::getZdId, bo.getZdId());
        lqw.eq(bo.getGysId() != null, MaMaterialManifest::getGysId, bo.getGysId());
        lqw.eq(bo.getMaStandardId() != null, MaMaterialManifest::getMaStandardId, bo.getMaStandardId());
        lqw.eq(bo.getType() != null, MaMaterialManifest::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getManifestCode()), MaMaterialManifest::getManifestCode, bo.getManifestCode());
        lqw.like(StringUtils.isNotBlank(bo.getManifestName()), MaMaterialManifest::getManifestName, bo.getManifestName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), MaMaterialManifest::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), MaMaterialManifest::getSpecification, bo.getSpecification());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), MaMaterialManifest::getUnit, bo.getUnit());
        lqw.eq(bo.getManifestNumber() != null, MaMaterialManifest::getManifestNumber, bo.getManifestNumber());
        lqw.eq(bo.getManifestPrice() != null, MaMaterialManifest::getManifestPrice, bo.getManifestPrice());
        lqw.eq(bo.getManifestTotal() != null, MaMaterialManifest::getManifestTotal, bo.getManifestTotal());
        lqw.eq(bo.getAdjustments() != null, MaMaterialManifest::getAdjustments, bo.getAdjustments());
        return lqw;
    }

    /**
     * 新增材料清单
     */
    @Override
    public Boolean insertByBo(MaMaterialManifestBo bo) {
        MaMaterialManifest add = BeanUtil.toBean(bo, MaMaterialManifest.class);
        // 根据供应商id获取站点id和标段id
        MaMaterialSupplierVo sp = supplierMapper.selectVoById(bo.getGysId());
        Long bdId = sp.getBdId();
        Long zdId = sp.getZdId();
        add.setBdId(bdId);
        add.setZdId(zdId);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setManifestId(add.getManifestId());
        }
        return flag;
    }

    /**
     * 修改材料清单
     */
    @Override
    public Boolean updateByBo(MaMaterialManifestBo bo) {
        MaMaterialManifest update = BeanUtil.toBean(bo, MaMaterialManifest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaMaterialManifest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除材料清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取清单列表-不分页
     *
     * @return
     */
    @Override
    public R<List<MaMaterialManifestVo>> allList(MaMaterialManifestBo bo) {
        LambdaQueryWrapper<MaMaterialManifest> lqw = buildQueryWrapper(bo);
        List<MaMaterialManifestVo> data = baseMapper.selectVoList(lqw);
        return R.ok(data);
    }

    /**
     * 批量新增材料清单
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean addList(MaMaterialManifestBo bo) {
        // 根据供应商id获取站点id和标段id
        MaMaterialSupplierVo sp = supplierMapper.selectVoById(bo.getGysId());
        Long bdId = sp.getBdId();
        Long zdId = sp.getZdId();
        List<MaMaterialManifest> addList = bo.getAddList();
        addList.forEach(item -> {
            item.setBdId(bdId);
            item.setZdId(zdId);
            item.setGysId(bo.getGysId());
        });
        return baseMapper.insertBatch(addList);
    }

    /**
     * 批量修改材料清单
     *
     * @param bo
     * @return
     */
    @Override
    public Boolean editList(MaMaterialManifestBo bo) {
        List<MaMaterialManifest> updateList = bo.getUpdateList();
        return baseMapper.updateBatchById(updateList);
    }
}
